/*
 * adf.h
 *
 *  Created on: 08.06.2012
 *      Author: Roma Jam
 */

#include "stdint.h"
#include "kl_lib.h"

#ifndef ADF_H_
#define ADF_H_

//********** HW & SW initalations of ADF1 (1424 MHz) ***************************
// HW
#define ADF1_PORT               GPIOA
#define ADF1_PORT_CLK           RCC_APB2Periph_GPIOA
#define ADF1_SCK                GPIO_Pin_5
//#define ADF1_MISO               GPIO_Pin_1
#define ADF1_MOSI               GPIO_Pin_7
#define ADF1_CS                 GPIO_Pin_8
#define ADF1_LD                 GPIO_Pin_11
// SW
#define ADF1_Freq               uint16_t(1424)
#define ADF1_INTvalue           uint16_t(142)
#define ADF1_FRACvalue          uint16_t(8)
#define ADF1_MODvalue           uint16_t(20)
#define ADF1_PHASEvalue         uint16_t(18)
#define ADF1_LitheraZero        int(-5)

//************ HW & SW initalations of ADF2 (248 MHz) *************************
// HW
#define ADF2_PORT               GPIOB
#define ADF2_PORT_CLK           RCC_APB2Periph_GPIOB
#define ADF2_SCK                GPIO_Pin_13
//#define ADF2_MISO               GPIO_Pin_1
#define ADF2_MOSI               GPIO_Pin_15
#define ADF2_CS                 GPIO_Pin_12
#define ADF2_LD                 GPIO_Pin_10
// SW
#define ADF2_Freq               uint16_t(248)
#define ADF2_INTvalue           uint16_t(24)
#define ADF2_FRACvalue          uint16_t(16)
#define ADF2_MODvalue           uint16_t(20)
#define ADF2_PHASEvalue         uint16_t(18)
#define ADF2_LitheraZero        int(-5)

//-----------------------------------------------------------------------------
//#define ADF_SCK_HI()            GPIO_SetBits(ADF_PORT, ADF_SCK)
//#define ADF_SCK_LO()            GPIO_ResetBits(ADF_PORT, ADF_SCK)
//#define ADF_MOSI_HI()           GPIO_SetBits(ADF_PORT, ADF_MOSI)
//#define ADF_MOSI_LO()           GPIO_ResetBits(ADF_PORT, ADF_MOSI)
//#define ADF_CS_HI()             GPIO_SetBits(ADF_PORT, ADF_CS)
//#define ADF_CS_LO()             GPIO_ResetBits(ADF_PORT, ADF_CS)

//********************* Struct Intro ************************************

struct AdfDescriptions_t {
// Hardware description
    GPIO_TypeDef *PORT;
    uint32_t RCC_CLK;
    uint16_t SCK;
    uint16_t MOSI;
    uint16_t CS;
    uint16_t LD;
// Software description
    uint16_t Freq;
    uint16_t Int;
    uint16_t Frac;
    uint16_t Mod;
    uint16_t Phase;
    int Lithera;
};

class adf_t {
private:
    uint32_t ATimer;
    bool LD_1424MHz_Yes, LD_248MHz_Yes;
    void ADF_SCK_HI(AdfDescriptions_t *PAdf)    { GPIO_SetBits(PAdf->PORT, PAdf->SCK); }
    void ADF_SCK_LO(AdfDescriptions_t *PAdf)    { GPIO_ResetBits(PAdf->PORT, PAdf->SCK); }
    void ADF_MOSI_HI(AdfDescriptions_t *PAdf)   { GPIO_SetBits(PAdf->PORT, PAdf->MOSI); }
    void ADF_MOSI_LO(AdfDescriptions_t *PAdf)   { GPIO_ResetBits(PAdf->PORT, PAdf->MOSI); }
    void ADF_CS_HI(AdfDescriptions_t *PAdf)     { GPIO_SetBits(PAdf->PORT, PAdf->CS); }
    void ADF_CS_LO(AdfDescriptions_t *PAdf)     { GPIO_ResetBits(PAdf->PORT, PAdf->CS); }
    bool LOCKDETECT(AdfDescriptions_t *PAdf)    { return GPIO_ReadInputDataBit(PAdf->PORT, PAdf->LD); }
public:
    uint32_t Timer;
    int Lit1424, lit248;
    AdfDescriptions_t ADF1_PINs, ADF2_PINs;
    void WriteToRegister(AdfDescriptions_t *PAdf, uint32_t ByteToWrite);
    void HardwareInit(AdfDescriptions_t *PAdf);
    void PortInit(void);
    void Task(void);
};
extern adf_t Adf;
extern AdfDescriptions_t *Adf1424Mhz , *Adf248MHz;

#endif /* ADF_H_ */
